package org.bing.zion.handler;

import java.nio.ByteBuffer;

import org.bing.zion.core.MessageHandler;
import org.bing.zion.core.MessageHandlerChain;
import org.bing.zion.core.Session;


public class EchoHandler implements MessageHandler {

	public void handle(MessageHandlerChain chain, Session session, Object msg) {
		if (msg instanceof ByteBuffer) {
			ByteBuffer buf = (ByteBuffer) msg;
			ByteBuffer tmp = buf.duplicate();
			tmp.flip();
			byte[] bs = new byte[tmp.limit()];
			tmp.get(bs);
			session.flush(ByteBuffer.wrap(bs));
		} else {
			session.write(msg);
		}

		chain.next().handle(chain, session, msg);
	}

}
